<?php 
require_once 'restaurantManagement.php';
require_once 'settingsManagement.php';
require_once 'profits.php';
require_once 'config.php';
require_once 'DataBase.php';
require_once 'User.php';
$statement=null;
$db = new DataBase();
$config = new Config();
$db->connect($config -> getHost(), $config -> getPort(), $config->getDbname(), $config->getUser(), $config->getPass());
$newUser=new User($db);
$editUser=new User($db);
?>
<!DOCTYPE html>
<html>
<head>
  <title>Panel Administratora</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8; width=device-width, initial-scale=1.0" >
  <link href="css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<?php
if (isset($_GET['uM'])) {	  
  if($_GET['uM']==1){
?>
	<div class="container" id="main-container">
	<div class="row" style="height: 150px;">
	  <div class="col-md-12"></div>
	</div>
	<div class="row">
	  <div class="col-md-3" id="top-left-menu"></div>
	  <div class="col-md-9" id="top-right-menu">
	    <div class="btn-group btn-group-justified">
		  <a href="userManagement.php?uM=1" type="button" class="btn btn-danger">Zarządzaj użytkownikami</a>
		  <a href="restaurantManagement.php?rM=1" type="button" class="btn btn-warning">Zarządzaj restauracjami</a>
		  <a href="settingsManagement.php?sM=1" type="button" class="btn btn-warning">Zarządzaj ustawieniami</a>
		  <a href="profits.php?p=1" type="button" class="btn btn-warning">Podgląd zysków</a>
		  <a type="button" class="btn btn-warning">Wyloguj</a>
		</div>
	  </div>
	</div>
    <div class="row">
        <div class="col-md-3" id="left-menu">
		  <div class="btn-group-vertical btn-group-block"> 
			<a href="userManagement.php?uM=1" type="button" class="btn btn-danger btn-block" style="width: 270px;">Znajdź użytkownika</a>
		    <a href="userManagement.php?uM=2" type="button" class="btn btn-warning btn-block" style="width: 270px;">Dodaj użytkownika</a>
			<a href="userManagement.php?uM=3" type="button" class="btn btn-warning btn-block" style="width: 270px;">Edytuj użytkownika</a>
		  </div>
		</div>
        <div class="col-md-9" id="body">
		  <div class="well">
		    <form class="form-search form-inline" action="userManagement.php?uM=1" method="POST">
			  <legend><center>Znajdź użytkownika</center></legend>
			  <center>			  
			    <select class="input" name="userManagementFilterChooser">
				  <option>Imię i nazwisko</option>
				  <option>Email</option>
				  <option>Telefon</option>
				  <option>Wszyscy użytkownicy</option>
			    </select>
			  <input type="text" class="input search-query" name="userManagementFilter">
			  <button type="submit" class="btn-primary" name="findUser">Szukaj</button>
			  </center>
			</form>
		  </div>
		  <div class="well">
			<form class="form-group" style="height: 290px">
<?php
	if(isset($_REQUEST['findUser'])) {
		if(isset($_POST['userManagementFilter'])){
			if(isset($_POST['userManagementFilterChooser'])){
				if($_POST['userManagementFilterChooser']=='Imię i nazwisko'){
					$statement=$db->query('select id, name, email, location, telNumber from users where name="'.$_POST["userManagementFilter"].'"');
				}
				if($_POST['userManagementFilterChooser']=='Email'){
					$statement=$db->query('select id, name, email, location, telNumber from users where email="'.$_POST["userManagementFilter"].'"');
				}
				if($_POST['userManagementFilterChooser']=='Telefon'){
					$statement=$db->query('select id, name, email, location, telNumber from users where telNumber="'.$_POST["userManagementFilter"].'"');
				}
				if($_POST['userManagementFilterChooser']=='Wszyscy użytkownicy'){
					$statement=$db->query('select id, name, email, location, telNumber from users');
				}
			}
		}
		foreach($statement as $row)
		{
          echo('<label class="col-sm-1 control-label">'.$row['id'].'</label>');
		  echo('<label class="col-sm-2 control-label"><center>'.$row['name'].'</center></label>');
		  echo('<label class="col-sm-2 control-label"><center>'.$row['email'].'</center></label>');
		  echo('<label class="col-sm-5 control-label"><center>'.$row['location'].'</center></label>');
		  echo('<label class="col-sm-2 control-label"><center>'.$row['telNumber'].'</center></label><hr>');
		  
		}
      $statement->closeCursor();
	}
?>			  
			  </label>
			</form>
		  </div>
		</div>
	</div>
  </div>
  </body>
  </html>
<?php
 }
  else if($_GET['uM']==2){
?>
	<div class="container" id="main-container">
	<div class="row" style="height: 150px;">
	  <div class="col-md-12"></div>
	</div>
	<div class="row">
	  <div class="col-md-3" id="top-left-menu"></div>
	  <div class="col-md-9" id="top-right-menu">
	    <div class="btn-group btn-group-justified">
		  <a href="userManagement.php?uM=1" type="button" class="btn btn-danger">Zarządzaj użytkownikami</a>
		  <a href="restaurantManagement.php?rM=1" type="button" class="btn btn-warning">Zarządzaj restauracjami</a>
		  <a href="settingsManagement.php?sM=1" type="button" class="btn btn-warning">Zarządzaj ustawieniami</a>
		  <a href="profits.php?p=1" type="button" class="btn btn-warning">Podgląd zysków</a>
		  <a type="button" class="btn btn-warning">Wyloguj</a>
		</div>
	  </div>
	</div>
    <div class="row">
        <div class="col-md-3" id="left-menu">
		  <div class="btn-group-vertical btn-group-block">
			<a href="userManagement.php?uM=1" type="button" class="btn btn-warning btn-block" style="width: 270px;">Znajdź użytkownika</a>
		    <a href="userManagement.php?uM=2" type="button" class="btn btn-danger btn-block" style="width: 270px;">Dodaj użytkownika</a>
			<a href="userManagement.php?uM=3" type="button" class="btn btn-warning btn-block" style="width: 270px;">Edytuj użytkownika</a>
		  </div>
		</div>
        <div class="col-md-9" id="body">
		  <div class="well">
		    <form class="form-horizontal" role="form" action="userManagement.php?uM=2" method="POST">
			  <div class="form-group">
			    <label for="userName" class="col-sm-2 control-label">Imię:</label>
			    <div class="col-sm-10">
				  <input type="text" class="form-control" id="userName" placeholder="Imie" name="addUserName">
		  	    </div>
			  </div>
			  <div class="form-group">
			    <label for="userSurname" class="col-sm-2 control-label" name>Nazwisko:</label>
			    <div class="col-sm-10">
			      <input type="text" class="form-control" id="userSurname" placeholder="Nazwisko" name="addUserSurname">
			    </div>
			  </div>
			  <div class="form-group">
			    <label for="email" class="col-sm-2 control-label">E-mail:</label>
			    <div class="col-sm-10">
			      <input type="email" class="form-control" id="email" placeholder="e-mail" name="addUserEmail">
			    </div>
			  </div>
			  <div class="form-group">
			    <label for="password" class="col-sm-2 control-label">Hasło</label>
			    <div class="col-sm-10">
			      <input type="text" class="form-control" id="password" placeholder="password" name="addUserPassword">
			    </div>
			  </div>
			  <div class="form-group">
			    <label for="address" class="col-sm-2 control-label">Adres zamieszkania:</label>
			    <div class="col-sm-10">
			      <input type="text" class="form-control" id="adres" placeholder="adres zamieszkania" name="addUseraddress">
			    </div>
			  </div>
			  <div class="form-group">
			    <label for="postCode" class="col-sm-2 control-label">Kod Pocztowy:</label>
			    <div class="col-sm-10">
			      <input type="text" class="form-control" id="postCode" placeholder="kod" name="addUserPostCode">
			    </div>
			  </div>
			  <div class="form-group">
			    <label for="city" class="col-sm-2 control-label">Miasto:</label>
			    <div class="col-sm-10">
			      <input type="text" class="form-control" id="city" placeholder="miasto" name="addUserCity">
			    </div>
			  </div>
			  <div class="form-group">
			    <label for="telNumber" class="col-sm-2 control-label">Numer telefonu:</label>
			    <div class="col-sm-10">
			      <input type="text" class="form-control" id="telNumber" placeholder="Numer telefonu" name="addUserTelNumber">
			    </div>
			  </div>
			  <div class="form-group">
			    <label class="col-sm-2 control-label">Ranga:</label>
			    <div class="col-sm-10">
			      <select class="input form-control" name="addUserRank">
				    <option>Użytkownik</option>
				    <option>Administrator Restauracji</option>
			      </select>
			    </div>
			  </div>
			  <div class="form-group">
			    <div>
				  <center>
				    <button type="submit" class="btn btn-primary" name="addButton">Dodaj Użytkownika</button>
			      </center>
				</div>
			  </div>
		    </form>
<?php
	if(isset($_REQUEST['addButton'])) {
		$temp;
		if(isset($_POST['addUserName'])){
			if(isset($_POST['addUserSurname'])){
				$temp=$_POST['addUserName'].' '.$_POST['addUserSurname'];
				$newUser->setName($temp);
			}
		}
		if(isset($_POST['addUserEmail'])){
			$newUser->setEmail($_POST['addUserEmail']);
		}
		if(isset($_POST['addUserPassword'])){
			$newUser->changePassword($_POST['addUserPassword']);
		}
		if(isset($_POST['addUseraddress'])){
			if(isset($_POST['addUserPostCode'])){
				if(isset($_POST['addUserCity'])){
					$temp=$_POST['addUseraddress'].', '.$_POST['addUserPostCode'].' '.$_POST['addUserCity'];
					$newUser->setLocation($temp);
				}
			}
		}
		if($_POST['addUserTelNumber']){
			$newUser->setTelNumber($_POST['addUserTelNumber']);
		}		
		$newUser->save();
		if($_POST['addUserRank']=='Administrator Restauracji'){
			//do zrobienia
		}
	}				
?>
		  </div>
		</div>
	</div>
  </div>
  </body>
  </html>
<?php
 }
  else{
?>
	<div class="container" id="main-container">
	<div class="row" style="height: 150px;">
	  <div class="col-md-12"></div>
	</div>
	<div class="row">
	  <div class="col-md-3" id="top-left-menu"></div>
	  <div class="col-md-9" id="top-right-menu">
	    <div class="btn-group btn-group-justified">
		  <a href="userManagement.php?uM=1" type="button" class="btn btn-danger">Zarządzaj użytkownikami</a>
		  <a href="restaurantManagement.php?rM=1" type="button" class="btn btn-warning">Zarządzaj restauracjami</a>
		  <a href="settingsManagement.php?sM=1" type="button" class="btn btn-warning">Zarządzaj ustawieniami</a>
		  <a href="profits.php?p=1" type="button" class="btn btn-warning">Podgląd zysków</a>
		  <a type="button" class="btn btn-warning">Wyloguj</a>
		</div>
	  </div>
	</div>
    <div class="row">
        <div class="col-md-3" id="left-menu">
		  <div class="btn-group-vertical btn-group-block"> 
			<a href="userManagement.php?uM=1" type="button" class="btn btn-warning btn-block" style="width: 270px;">Znajdź użytkownika</a>
		    <a href="userManagement.php?uM=2" type="button" class="btn btn-warning btn-block" style="width: 270px;">Dodaj użytkownika</a>
			<a href="userManagement.php?uM=3" type="button" class="btn btn-danger btn-block" style="width: 270px;">Edytuj użytkownika</a>
		  </div>
		</div>
        <div class="col-md-9" id="body">
		  <div class="well">
		    <form class="form-search form-inline" action="userManagement.php?uM=3" method="POST">
			  <legend><center>Wybierz użytkownika do edycji</center></legend>
			  <center>			  
			    <select class="input" name="userManagementEditFilterChooser">
				  <option>Id</option>
				  <option>Email</option>
				  <option>Telefon</option>
			    </select>
			  <input type="text" class="input search-query" name="userManagementEditFilter">
			  <button type="submit" class="btn-primary" name="findUserToEdit">Wybierz</button>
			  <button type="submit" class="btn-primary" name="deleteUser">Usuń</button>
			  </center>
			</form>
		  </div>
<?php
	if(isset($_REQUEST['findUserToEdit'])) {
		if(isset($_POST['userManagementEditFilter'])){
			if(isset($_POST['userManagementEditFilterChooser'])){
				if($_POST['userManagementEditFilterChooser']=='Id'){
					$statement=$db->query('select * from users where id='.$_POST["userManagementEditFilter"]);
				}
				if($_POST['userManagementEditFilterChooser']=='Email'){
					$statement=$db->query('select * from users where email='.$_POST["userManagementEditFilter"]);
				}
				if($_POST['userManagementEditFilterChooser']=='Telefon'){
					$statement=$db->query('select * from users where telNumber='.$_POST["userManagementEditFilter"]);
				}
				foreach($statement as $row)
				  {
					$editUser->setName($row['name']);
					$editUser->setEmail($row['email']);
					$editUser->setTelNumber($row['telNumber']);
					$editUser->setLocation($row['location']);
				  }
				$statement->closeCursor();
			}
		}
	}
	if(isset($_REQUEST['deleteUser'])) {
		if(isset($_POST['userManagementEditFilter'])){
			if(isset($_POST['userManagementEditFilterChooser'])){
				if($_POST['userManagementEditFilterChooser']=='Id'){
					$db->exec('delete from users where id="'.$_POST["userManagementEditFilter"].'"');
				}
				if($_POST['userManagementEditFilterChooser']=='Email'){
					$db->query('delete from users where email="'.$_POST["userManagementEditFilter"].'"');
				}
				if($_POST['userManagementEditFilterChooser']=='Telefon'){
					$db->query('delete from users where telNumber="'.$_POST["userManagementEditFilter"].'"');
				}
			}
		}
	}
	
?>
		  <div class="well">
		    <form class="form-horizontal" role="form" action="userManagement.php?uM=3" method="POST">
			  <div class="form-group">
			    <label for="userName" class="col-sm-2 control-label">Imię i nazwisko:</label>
			    <div class="col-sm-10">
				  <input type="text" class="form-control" id="userName" name="editUserName" value="
<?php
echo($editUser->getName());
?>
				  ">
		  	    </div>
			  </div>
			  <div class="form-group">
			    <label for="email" class="col-sm-2 control-label">E-mail:</label>
			    <div class="col-sm-10">
			      <input type="email" class="form-control" id="email" name="editUserEmail" value="
<?php
echo($editUser->getEmail());
?>					  
				  ">
			    </div>
			  </div>
			  <div class="form-group">
			    <label for="address" class="col-sm-2 control-label">Adres zamieszkania:</label>
			    <div class="col-sm-10">
			      <input type="text" class="form-control" id="adres" name="editUserAddress" value="
<?php
echo($editUser->getLocation());
?>	  				  
				  ">
			    </div>
			  </div>
			  <div class="form-group">
			    <label for="telNumber" class="col-sm-2 control-label">Numer telefonu:</label>
			    <div class="col-sm-10">
			      <input type="text" class="form-control" id="telNumber" placeholder="Numer telefonu" name="editUserTelNumber" value="
<?php
echo($editUser->getTelNumber());
?>					  
				  ">
			    </div>
			  </div>
			  <div class="form-group">
			    <label class="col-sm-2 control-label">Ranga:</label>
			    <div class="col-sm-10">
			      <select class="input form-control" name="editUserRank">
				    <option>Użytkownik</option>
				    <option>Administrator Restauracji</option>
			      </select>
			    </div>
			  </div>
			  <div class="form-group">
			    <div>
				  <center>
				    <button type="submit" class="btn btn-primary" name="editButton">Edytuj Użytkownika</button>
			      </center>
				</div>
			  </div>
		    </form>
<?php
if(isset($_REQUEST['editButton'])) {
		if(isset($_POST['editUserName'])){
			$editUser->setName($_POST['editUserName']);
		}
		if(isset($_POST['editUserEmail'])){
			$editUser->setEmail($_POST['editUserEmail']);
		}
		if(isset($_POST['editUserAddress'])){
			$editUser->setLocation($_POST['editUserAddress']);
		}
		if($_POST['editUserTelNumber']){
			$editUser->setTelNumber($_POST['editUserTelNumber']);
		}		
		echo('update users set name='.$editUser->getName().', email='.$editUser->getEmail().', location='.$editUser->getLocation().', telNumber='.$editUser->getTelNumber().' where id=5');
		if($_POST['editUserRank']=='Administrator Restauracji'){
			//do zrobienia
		}
	}	
?>		  
		  </div>
		</div>
	</div>
  </div>
  </body>
  </html>
  <?php
  }
}
?>